using System;

using Microsoft.SPOT;

using mfRCF.Core;
using mfRCF.Core.Extensions;

using mfXmlSerializer;

using mfLogger;

using OST.Shared;
using OST.Motor;
using OST.Anemometer;
using OST.Calc.Tracking;
using OST.Commands.Extensions;

namespace OST.Commands
{

    /// <summary>
    /// starts the automatich sun tracking
    /// </summary>
    public class TrackerStartGETCommand : mfRcfCommand
    {
        #region CTOR

        public TrackerStartGETCommand(string httpPath)
            : base(httpPath, new mfGETMethod())
        {
        }

        #endregion

        #region Properties

        /// <summary>
        /// wrapper-property 4 logging
        /// </summary>
        private ILoggerAppender Logger
        {
            get
            {
                return mfLogger.mfLogger.Instance;
            }
        }

        #endregion

        #region mfRcfCommand
        public override void ExecuteCommand(mfIHttpContext httpContext)
        {
            try
            {
                if (TrackerProvider.Instance == null
                    || TrackerProvider.Instance != null && !TrackerProvider.Instance.HasInstance)
                    throw new OSTException("no tracker instance found");

                if (TrackerProvider.Instance.TrackerInstance.IsStarted)
                {
                    this.WriteHttpResponseOK(httpContext, "tracker already started");
                    return;
                }

                TrackerProvider.Instance.TrackerInstance.Start();

                this.WriteHttpResponseOK(httpContext, "tracker started");
            }
            catch (Exception ex)
            {
                this.WriteHttpResponseError(httpContext, ex);
            }
        }

        #endregion
    }
}
